package com.study;

import com.alibaba.fastjson.JSON;
import com.study.dto.Search;

import javax.json.Json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxl
 * @version 2022-06-16 15:03
 */

public class R<T> {
    private Boolean ok;
    private String message;
    private Integer code;
    private T data;

    public R() {
        this.ok = true;
    }

    public R(Boolean ok) {
        this.ok = ok;
    }

    public R(Boolean ok, String message, Integer code, T data) {
        this.ok = ok;
        this.message = message;
        this.code = code;
        this.data = data;
    }

    public R(String message, Integer code, T data) {
        this.ok = false;
        this.message = message;
        this.code = code;
        this.data = data;
    }

    public static <T> R<T> ok() {
        return new R<T>();
    }

    public static <T> R<T> error() {
        return new R<T>(false);
    }

    public Boolean getOk() {
        return ok;
    }

    public String getMessage() {
        return message;
    }

    public Integer getCode() {
        return code;
    }

    public T getData() {
        return data;
    }

    public R<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public R<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public R<T> setData(T data) {
        /*try {
            this.data = data;
            if(data == null)
                this.ok = false;
            // 用
            if(data instanceof Integer) {
                Integer row = (Integer)data;
                if(row == 0) {
                    this.ok = false;
                    this.message = "受影响的的行数为:0";
                }
            }
        }catch (RuntimeException e) {
            this.ok = false;
            String temp = "服务器运行时错误";
            this.data = (T) temp; 
            e.printStackTrace();
        } catch (Exception e) {
            this.ok = false;
            String temp = "服务器运行时错误";
            this.data = (T) temp;
            e.printStackTrace();
        }*/
        this.data = data;
        if(data == null)
            this.ok = false;
        // 用
        if(data instanceof Integer) {
            Integer row = (Integer)data;
            if(row == 0) {
                this.ok = false;
                this.message = "受影响的的行数为:0";
            }
        }
        return  this;
    }

    /**
     * 获取一个data类型为map的结果
     * @param ok
     * @return
     */
    public static R<?> map(boolean ok) {
        Map<String, Object> map = new HashMap<>();
        return new R<Map<String, Object>>(ok,null,null,map);
    }

    /**
     * 获取一个data类型为map的结果
     * @return
     */
    public static R<?> map() {
        return map(true);
    }

    @SuppressWarnings("unchecked")
    public R<?> put(String key, Object value) {
        if(this.data instanceof Map) {
            ((Map<String, Object>)this.data).put(key,value);
            return this;
        }else 
            throw new RuntimeException("data does not belong to Map");
    }

    /**
     * 获取data类型为List的数据
     * @param ok
     * @return
     */
    public static R<?> list(boolean ok) {
        List<Object> list = new ArrayList<>();
        return new R<List<Object>>(ok,null,null,list);
    }
    
    public static R<?> list() {
        return list(true);
    }
    
    @SuppressWarnings("unchecked")
    public R<?> add(Object value) {
        if(this.data instanceof List) {
            ((List<Object>)this.data).add(value);
            return this;
        }else 
            throw new RuntimeException("data does not belong to List");
    }
}
